﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Appkiz.Library.Security.Authentication;
using Appkiz.Library.Security;
using Tiger.Common;
using Tiger.Models;

namespace DQC.Controllers
{
    public class OpinionController : Controller
    {
        private OrgMgr mgr = new OrgMgr();
        private WhDbContext whdb = new WhDbContext();

        /// <summary>
        /// 获取个人意见
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPrivateOpinion()
        {
            try
            {
                // 当前用户
                var employee = (User.Identity as AppkizIdentity).Employee;

                // 查询当前用户的所有个人意见
                var PrivateOpinions = whdb.PrivateOpinion.Where(p => p.EmplID == employee.EmplID).Select(p => new {
                    id = p.ID,
                    content = p.Content
                } ).ToList();

                return new JsonNetResult(new
                {
                    Succeed = true,
                    Data = new
                    {
                        opinions = PrivateOpinions
                    }
                });
            }
            catch(Exception e)
            {
                return ResponseUtil.Error(400, e.Message);
            }
        }

        /// <summary>
        /// 获取个人意见（移动端）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPrivateOpinionMobile()
        {
            try
            {
                // 当前用户
                var employee = (User.Identity as AppkizIdentity).Employee;

                // 查询当前用户的所有个人意见
                var PrivateOpinions = whdb.PrivateOpinion.Where(p => p.EmplID == employee.EmplID).Select(p => p.Content).ToList();

                return new JsonNetResult(new
                {
                    Succeed = true,
                    Data = new
                    {
                        opinions = PrivateOpinions
                    }
                });
            }
            catch (Exception e)
            {
                return ResponseUtil.Error(400, e.Message);
            }
        }

        /// <summary>
        /// 插入个人意见
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddPrivateOpinion(FormCollection collection)
        {
            try
            {
                /*
                 * 参数获取
                 */
                // 意见
                var content = collection["content"];
                // 当前用户
                var employee = (User.Identity as AppkizIdentity).Employee;

                /*
                 * 参数校验
                 */
                // 内容校验
                if(string.IsNullOrEmpty(content))
                {
                    return new JsonNetResult(new
                    {
                        Succeed = false,
                        Message = "意见不能为空"
                    });
                }

                /*
                 * 插入意见到数据库
                 */
                TGR_PrivateOpinion privateOpinion = new TGR_PrivateOpinion();
                privateOpinion.EmplID = employee.EmplID;
                privateOpinion.Content = content;
                privateOpinion.CreateTime = DateTime.Now;
                privateOpinion.UpdateTime = DateTime.Now;

                whdb.PrivateOpinion.Add(privateOpinion);
                whdb.SaveChanges();

                return new JsonNetResult(new
                {
                    Succeed = true,
                    Message = "添加个人意见成功"
                });
            }
            catch(Exception e)
            {
                return new JsonNetResult(new
                {
                    Succeed = false,
                    Message = e.Message
                });
            }
        }

        /// <summary>
        /// 删除个人意见
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RemovePrivateOpinion(FormCollection collection)
        {
            /*
             * 变量声明
             */
            int opinionId = 0;
            /*
             * 参数获取
             */
            // 意见ID
            var id = collection["id"];

            /*
             * 参数校验
             */
            if(string.IsNullOrEmpty(id))
            {
                return new JsonNetResult(new
                {
                    Succeed = false,
                    Message = "意见ID不能为空"
                });
            }
            if(!int.TryParse(id, out opinionId))
            {
                return new JsonNetResult(new
                {
                    Succeed = false,
                    Message = "意见ID应该为数字"
                });
            }
            else
            {
                var opinion = whdb.PrivateOpinion.Where(p => p.ID == opinionId).FirstOrDefault();
                if(opinion == null)
                {
                    return new JsonNetResult(new
                    {
                        Succeed = false,
                        Message = "意见不存在"
                    });
                }
                else
                {
                    whdb.PrivateOpinion.Remove(opinion);
                    whdb.SaveChanges();
                    return new JsonNetResult(new
                    {
                        Succeed = true,
                        Message = "删除成功"
                    });
                }
            }
        }

        /// <summary>
        /// 获取公共意见
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPublicOpinion()
        {
            var publicOpinions = whdb.PublicOpinion.Select(p => p.Content).ToList();

            return new JsonNetResult(new
            {
                Succeed = true,
                Data = new
                {
                    opinions = publicOpinions
                }
            });
        }
    }
}